/*
 * Person.java
 *
 * Created on 12. August 2007, 23:36
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package timeitj.persistence;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.persistence.Version;
import org.apache.log4j.Logger;

/**
 *
 * @author kunee
 */
@Entity
@NamedQuery(
name="PersonByLogin",
    query="SELECT p From Person p WHERE p.login = :login")
    
    @EntityListeners(timeitj.persistence.monitors.PersonMonitor.class)
    @Table(name="PERSON",
uniqueConstraints=
{@UniqueConstraint(columnNames={"LOGIN","EMAIL"})})

public class Person implements Serializable {
    
    private static Logger logger = Logger.getLogger(Person.class.getName());
    
    @Id @GeneratedValue
    @Column(name="ID")
    private long id;
    
    @Version
    @Column(name="OPT_LOCK")
    private Long version;
    
    @Column(name="LOGIN")
    private String login;
    
    @Column(name="PASSWORD")
    private String password;
    
    @Column(name="FIRST_NAME")
    private String firstName;
    
    @Column(name="LAST_NAME")
    private String lastName;
    
    @Column(name="EMAIL")
    private String email;
    
    @ManyToMany
    @Column(name="GROUPS")
    private List<Group> groups;
    
    @OneToMany(mappedBy="inviter", cascade=CascadeType.ALL)
    private List<Invitation> invitationsFrom;
    
    @OneToMany(mappedBy="invitee")
    private List<Invitation> invitationsTo;
    
    //TODO: wieso eager?
    @OneToMany(mappedBy = "owner", fetch=FetchType.EAGER,
    cascade = {CascadeType.MERGE,CascadeType.REMOVE})
    private List<Project> ownedProjects;
    
    /**
     * Creates a new instance of Person
     */
    public Person() {
        
        groups        = new ArrayList<Group>();
        ownedProjects = new ArrayList<Project>();
        invitationsFrom   = new ArrayList<Invitation>();
        invitationsTo   = new ArrayList<Invitation>();
        
    }
    
    public String getName() {
        return new String(firstName + " " + lastName);
    }
    
    public long getId() {
        return id;
    }
        
    public String getLogin() {
        return login;
    }
    
    public void setLogin(String login) {
        this.login = login;
    }
    
    public String getPassword() {
        return password;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    
    public String getFirstName() {
        return firstName;
    }
    
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    
    public String getLastName() {
        return lastName;
    }
    
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    
    public String getEmail() {
        return email;
    }
    
    public void setEmail(String email) {
        this.email = email;
    }
    
    public List<Group> getGroups(){
        return groups;
    }
    
    public List<Project> getOwnedProjects() {
        return ownedProjects;
    }
    
    public List<Invitation> getInvitationsFrom(){
        return invitationsFrom;
    }
    
    public void invite(Invitation invitation){
        invitationsTo.add(invitation);
    }
    
    public void addGroup(Group group) {
        groups.add(group);
        logger.info("Person '" + login + "' joined Group '" + group.getGroupName() + "'");
    }
    
    public void removeGroup(Group group){
        groups.remove(group);
        logger.info("Person '" + login + "' left Group '" + group.getGroupName() + "'");
    }
    
    public void addOwnedProject(Project project) {
        ownedProjects.add(project);
        logger.info("Person '" + login + "' is now owner of Project '" + project.getName() + "'");
    }
    
    public void removeOwnedProject(Project project) {
        ownedProjects.remove(project);
        logger.info("Person '" + login + "' is no longer owner of Project '" + project.getName() + "'");
    }
}
